package si.unimb.src.wifisms.ui;

import java.util.ArrayList;

import si.unimb.src.wifisms.R;
import si.unimb.src.wifisms.util.SettingsType;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CheckboxIconArrayAdapter extends ArrayAdapter<CheckableItem> {
	
	private Context context;
	private ArrayList<CheckableItem> items;
	private int resourceId;
	
    public CheckboxIconArrayAdapter(Context context, int textViewResourceId, ArrayList<CheckableItem> items) {
            super(context, textViewResourceId, items);
            this.context = context;
            this.items = items;
            this.resourceId = textViewResourceId;
    }
    
    @Override
	public int getCount() {
        return this.items.size();
    }

    @Override
	public CheckableItem getItem(int position) {
        return this.items.get(position);
    }

    @Override
	public long getItemId(int position) {
        return position;
    }
    
    @Override
	public View getView(int position, View convertView, ViewGroup parent) {
    	
    	final CheckableItem item = this.items.get(position);
		
		View v = convertView;
		if (v == null) {
		    LayoutInflater vi = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    v = vi.inflate( this.resourceId, null);
		}
		
		TextView txtName = (TextView)v.findViewById(R.id.name);
		txtName.setText(item.getName());
		
		ImageView iv = (ImageView)v.findViewById(R.id.checked_icon);
		
		SettingsType type = item.getType();
		if (type == SettingsType.WIFI)
			iv.setImageResource(item.isChecked() ? R.drawable.wifi_on : R.drawable.wifi_off);
		else
			iv.setImageResource(item.isChecked() ? R.drawable.edge_3g_on : R.drawable.edge_3g_off);
		
		return v;
    }


}
